package com.bookSystem.dao.Impl;/*
 * @Author:lsx
 * @Date:2024/9/14--09--14--17:09
 *
 */

import com.bookSystem.dao.BaseDao;
import com.bookSystem.dao.UserDao;
import com.bookSystem.entity.User;

import javax.swing.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDaoImpl extends BaseDao implements UserDao {
    BaseDao baseDao=new BaseDao();
    @Override
    public int login(String account, String password) {
        String sql="select * from user where account=?and userPass=?";
        Object[] params={account,password};
        if(baseDao.queryResultSet(sql, params)!=null){
            return 1;
        }
        return -1;
    }

    @Override
    public int register(String username, String password, String account) {
        String sql="insert into user(username,userPass,account) values(?,?,?)";
        Object[] params={username,password,account};
        if(baseDao.executeUpdate(sql, params)>0){
            return 1;
        }
        return -1;
    }

    @Override
    public User selectByAccountAndUserPass(String account, String password) {
        String sql="select * from user where account= ? and userPass= ?";
        Object[] params = {account, password};
        ResultSet rs = super.queryResultSet(sql, params);
        try {
            if (rs != null) {
                if (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("userId"));
                    user.setUsername(rs.getString("userName"));
                    user.setPassword(rs.getString("userPass"));
                    user.setAccount(rs.getString("account"));
                    return user;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 在这里关闭资源
            BaseDao.closeAll(super.conn, super.ps,rs);
        }

        return null;
    }

    @Override
    public int selectByAccount(String account) {
        String sql="select * from user where account= ?";
        Object[] params={account};
        ResultSet rs = super.queryResultSet(sql, params);
        try {
            if (rs != null && rs.next()) {

                int userId = rs.getInt("userId");
                String username = rs.getString("userName");
                return 1;  // 返回成功标志
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeAll(super.conn, super.ps,rs);
        }
        return -1;
    }
}
